﻿@using SIGBugs.Shared.Models
@using SIGBugs.Client.Libs;

@page "/editproject/{id}"
@inject HttpClient Http
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h2>编辑项目</h2>

<hr />
<div class="row">
    <div class="col-md-4">
        <form>
            @if (!string.IsNullOrEmpty(errorMessage))
            {
                <div class="alert alert-danger">
                    <span class="oi oi-circle-x"></span>  @errorMessage
                </div>

            }
            <div class="form-group @(isInvalidName?"was-validated":"")">
                <label for="Name" class="control-label">项目名称</label>
                <input for="Name" ref="inputName" class="form-control" bind="@project.Name" required />
                <div class="invalid-feedback">
                    请输入项目名称
                </div>
            </div>
            <div class="form-group">
                <label asp-for="Gender" class="control-label">Gender</label>
                <textarea asp-for="Description" bind="project.Description" class="form-control"></textarea>
            </div>

            <div class="form-group">
                <input type="submit" value="保存" onclick="@(async () => await UpdateProject())" class="btn btn-primary" />
                <input type="submit" value="取消" onclick="@cancel" class="btn" />
            </div>
        </form>
    </div>
</div>

@functions {
[Parameter]
string id { get; set; }

bool isInvalidName = false;
ElementRef inputName;

string errorMessage = string.Empty;
Project project = new Project();
protected override async System.Threading.Tasks.Task OnInitAsync()
{
    project = await Http.GetJsonAsync<Project>("/api/Projects/" + Convert.ToInt32(id));
}
protected async System.Threading.Tasks.Task UpdateProject()
{
    if (string.IsNullOrEmpty(project.Name))
    {
        isInvalidName = true;
        inputName.Focus();
        return;

    }

    await Http.SendJsonAsync(HttpMethod.Put, "api/Projects/" + Convert.ToInt32(id), project);
    UriHelper.NavigateTo("/fetchproject");
}
void cancel()
{
    UriHelper.NavigateTo("/fetchproject");
}
}  